home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pcmagazi / 1989 / 21 / mpidiv.asm < prev    next >
Assembly Source File  |  1989-10-31  |  4KB  |  105 lines

  1.         title   MPIDIV.ASM Multiple-Precision Signed Divide
  2.         page    55,132
  3.  
  4. ; MPIDIV.ASM    Multiple-Precision Signed Division
  5. ;               for Intel 8086, 8088, 80286, and
  6. ;               80386 in real mode/16-bit protected mode.
  7. ;               Requires MPNEG.ASM (multiple-precision
  8. ;               2's complement) and MPDIV.ASM (multiple-
  9. ;               precision unsigned integer divide).
  10. ;
  11. ; Copyright (c) 1989 Ziff Davis Communications
  12. ; PC Magazine * Ray Duncan
  13. ;
  14. ; Call with:    DS:SI   = address of divisor
  15. ;               ES:DI   = address of dividend
  16. ;               CX      = divisor length in bytes
  17. ;                         (dividend length = 2 * divisor length)
  18. ;
  19. ;               Assumes direction flag is clear at entry
  20. ;               Assumes DS = ES <> SS
  21. ;               Assumes 0 < CX <= 255
  22. ;
  23. ; Returns:      ES:DI   = address of quotient
  24. ;               DS:SI   = address of remainder
  25. ;
  26. ;               NOTE: Dividend is assumed to be twice as long
  27. ;               as the divisor.  Returned remainder and quotient 
  28. ;               are same size as divisor.
  29. ;
  30. ;               The sign of the quotient is positive if the signs
  31. ;               signs of the dividend and divisor are the same;
  32. ;               negative if they are different.  The sign of the
  33. ;               remainder is the same as the sign of the dividend.
  34. ;
  35. ; Destroys:     AX (other registers preserved)
  36.  
  37. _TEXT   segment word public 'CODE'
  38.  
  39.         extrn   mpdiv:near 
  40.         extrn   mpneg:near
  41.  
  42.         assume  cs:_TEXT
  43.  
  44.         public  mpidiv
  45. mpidiv  proc    near
  46.  
  47.         push    bx                      ; save registers
  48.  
  49.         mov     bx,cx                   ; get Exclusive-OR of
  50.         mov     al,[si+bx-1]            ; signs of operands
  51.         add     bx,bx
  52.         xor     al,[di+bx-1]
  53.         pushf                           ; save sign of result           
  54.  
  55.         mov     al,[di+bx-1]            ; test sign of dividend
  56.         or      al,al
  57.         pushf                           ; save sign of remainder
  58.  
  59.         jns     mpid1                   ; jump if dividend positive
  60.  
  61.         push    si                      ; save pointer to divisor
  62.         push    cx                      ; save length of divisor
  63.  
  64.         mov     si,di                   ; point to dividend
  65.         add     cx,cx                   ; calc length of dividend
  66.         call    mpneg                   ; flip sign of dividend
  67.  
  68.         pop     cx                      ; restore length of divisor
  69.         pop     si                      ; restore address of divisor
  70.  
  71. mpid1:  mov     bx,cx                   ; check if divisor negative
  72.         test    byte ptr [si+bx-1],80h
  73.         jz      mpid2                   ; jump, divisor is positive
  74.  
  75.         push    di                      ; save pointer to dividend
  76.         call    mpneg                   ; flip sign of divisor
  77.         pop     di                      ; restore pointer to dividend
  78.  
  79. mpid2:  call    mpdiv                   ; perform unsigned divide
  80.  
  81.         popf                            ; retrieve sign of remainder
  82.         jns     mpid3                   ; jump, remainder is positive
  83.  
  84.         push    di                      ; save pointer to quotient
  85.         call    mpneg                   ; flip sign of remainder
  86.         pop     di                      ; restore pointer to quotient
  87.  
  88. mpid3:  popf                            ; retrieve sign of result
  89.         jns     mpid4                   ; jump, result is positive
  90.  
  91.         push    si                      ; save pointer to remainder
  92.         mov     si,di                   ; point to quotient
  93.         call    mpneg                   ; flip sign of quotient
  94.         pop     si                      ; restore pointer to remainder
  95.  
  96. mpid4:  pop     bx                      ; restore register
  97.         ret                             ; back to caller
  98.  
  99. mpidiv  endp
  100.  
  101. _TEXT   ends
  102.  
  103.         end
  104.  
  105.